package cap.action;

import cap.bean.Category;
import cap.dao.CategoryDao;
import cap.dao.impl.CategoryDaoImpl;
import cap.util.PageControl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/category")
public class CategoryServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;
    private CategoryDao categoryDao;
    public CategoryServlet() {
        categoryDao=new CategoryDaoImpl();
    }
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf8");
        response.setCharacterEncoding("utf-8");
        String method=request.getParameter("method");
        if(("manage").equals(method)){//管理个人分类
            response.setCharacterEncoding("utf-8");
            int userId = Integer.parseInt(request.getParameter("userId"));
            int total=categoryDao.getByUserId(userId).size();
            //添加用于分类
            String curPageStr = request.getParameter("curPage");
            int curPage=1;
            if(curPageStr!=null){
                curPage=Integer.parseInt(curPageStr);
            }
            //个人分类
            PageControl pc = new PageControl(curPage, total);
            List<Category> cgList =categoryDao.getCategoryByPageUserId(curPage, total, userId);
            pc.setList(categoryDao.getByUserId(userId));
            request.setAttribute("curPage", pc.getCurPage());
            request.setAttribute("totalPages", pc.getTotalPages());
            request.setAttribute("cgList", pc.getList());
            request.getRequestDispatcher("front/CategoryManage.jsp").forward(request, response);
        }else if(method.equals("add")){//添加个人文章分类
            int userId = Integer.parseInt(request.getParameter("userId"));
            String cgName = request.getParameter("category_name");
            Category cg = categoryDao.getByName(cgName, userId);
            if (null == cg) {	//允许新建该分类
                int res = categoryDao.insertCategory(userId, cgName);
                if (res > 0) {
                    request.getSession().setAttribute("succAddMsg", "添加分类成功！");
                    response.sendRedirect("category?method=manage&userId="+userId);
                } else {
                    request.getSession().setAttribute("errorAddMsg", "添加分类失败！");
                    response.sendRedirect("category?method=manage&userId="+userId);
                }
            } else {//该分类已存在
                request.getSession().setAttribute("errorAddMsg", "该分类名已存在，请重新填写！");
                response.sendRedirect("front/AddCategory.jsp");
            }
        }else if(method.equals("edit")){//编辑个人文章分类
            int cgId = Integer.parseInt(request.getParameter("cgId"));
            Category cg = categoryDao.getById(cgId);
            request.setAttribute("cg", cg);
            request.getRequestDispatcher("front/EditCategory.jsp").forward(request, response);
        }else if(method.equals("save")){//更新个人文章分类
            int cgId = Integer.parseInt(request.getParameter("cgId"));
            int userId = Integer.parseInt(request.getParameter("userId"));
            String cgName = request.getParameter("category_name");
            Category cg = categoryDao.getByName(cgName, userId);
            if (null == cg) {//允许更新该分类
                int res = categoryDao.updateCategory(cgId, cgName);
                if (res > 0) {
                    request.getSession().setAttribute("succUpdateMsg", "更新分类成功！");
                    response.sendRedirect("category?method=manage&userId="+userId);
                } else {
                    request.getSession().setAttribute("errorUpdateMsg", "更新分类失败！");
                    response.sendRedirect("category?method=manage&userId="+userId);
                }
            } else {//该分类已存在，不允许更新该分类
                if (cgId != cg.getId()) {//查询的id和cgId不能一样
                    request.getSession().setAttribute("errorUpdateMsg", "该分类名已存在，请重新填写！");
                }
                response.sendRedirect("category?method=edit&cgId="+cgId);
            }
        }else if(method.equals("delete")){//删除个人文章分类
            int cgId = Integer.parseInt(request.getParameter("cgId"));
            int userId = Integer.parseInt(request.getParameter("userId"));
            int res = categoryDao.deleteCategory(cgId);
            if (res > 0) {	//删除成功
                request.getSession().setAttribute("succDeleMsg", "删除分类成功！");
            } else {
                request.getSession().setAttribute("errorDeleMsg", "删除分类失败！");
            }
            response.sendRedirect("category?method=manage&userId="+userId);
        }
    }
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }
}
